package com.tn.handler;

import com.tn.exception.InvalidProducerParamException;
import com.tn.controller.alarm.common.model.OutputVO;
import com.tn.controller.alarm.common.constants.Constants;
import com.tn.exception.ProducerCommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: tn_aucy_java
 * @description: 生产系统异常处理
 * @author: yangjiayao
 * @create: 2021-11-12 15:20
 */
@Slf4j
@ControllerAdvice
public class ProducerExceptionHandler {
    /**
     * 生产系统认证不通过异常处理
     *
     * @param e 异常
     * @return 返回消息
     */
    @ExceptionHandler(InvalidProducerParamException.class)
    @ResponseBody
    public OutputVO<Object> handle(InvalidProducerParamException e) {
        log.error("Invalid producer param!");
        OutputVO<Object> response = new OutputVO<>();
        response.setStatus(Constants.OtherSys.STATUS.INVALID.getCode());
        response.setInfo(Constants.OtherSys.STATUS.INVALID.getMessage());
        return response;
    }

    /**
     * 生产系统通用异常处理
     *
     * @param e 异常
     * @return 返回消息
     */
    @ExceptionHandler(ProducerCommonException.class)
    @ResponseBody
    public OutputVO<Object> handle(ProducerCommonException e) {
        log.error("Producer common exception!");
        OutputVO<Object> response = new OutputVO<>();
        response.setStatus(e.getCode());
        response.setInfo(e.getMessage());
        return response;
    }
}
